package com.charon.common.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
  *@ClassName: BaseController
  *@Description: 根：控制器
  *@Author: Charon []
  *@Date: 2019/1/11 0011 19:35
  *@Version: 1.0
  */
public abstract class BaseController {

    private final static Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    /**
     * 统一异常处理
     * @param request
     * @param response
     * @param exception
     */
    @ExceptionHandler
    public String exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
        LOGGER.error("统一异常处理：", exception);
        request.setAttribute("ex", exception);
        if (null != request.getHeader("X-Requested-With") && "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
            request.setAttribute("requestHeader", "ajax");
        }
        // shiro没有权限异常
        //if (exception instanceof UnauthorizedException) {
        //    return "/403.jsp";
        //}
        // shiro会话已过期异常
        //if (exception instanceof InvalidSessionException) {
        //    return "/error.jsp";
        //}
        return "/error.jsp";
    }

}
